package com.marc.eden.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.view.Surface;
import android.view.TextureView;

import java.util.Arrays;

public class MyCamera {

    CameraManager myCameraManager;
    CameraCharacteristics myCameraCharacteristics;
    String[] myCameraIdList;
    CaptureRequest.Builder myPreviewBuilder;
    TextureView myPreviewView;
    Handler myCameraHandler;

    private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            try {
                startPreview(camera);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onDisconnected(CameraDevice camera) {}

        @Override
        public void onError(CameraDevice camera, int error) {}
    };

    private CameraCaptureSession.StateCallback mySessionStateCallback = new CameraCaptureSession.StateCallback() {
        @Override
        public void onConfigured(CameraCaptureSession session) {
            try {
                //session.capture(mPreviewBuilder.build(), mSessionCaptureCallback, mHandler);
                session.setRepeatingRequest(myPreviewBuilder.build(), mySessionCaptureCallback, myCameraHandler);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onConfigureFailed(CameraCaptureSession session) {}
    };

    private CameraCaptureSession.CaptureCallback mySessionCaptureCallback =
            new CameraCaptureSession.CaptureCallback() {

                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                                               TotalCaptureResult result) {}

                @Override
                public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                                CaptureResult partialResult){}
    };

    public MyCamera() {}

    public void openMyCamera(Handler handler, TextureView textureView) {
        myPreviewView = textureView;
        try {
            myCameraIdList = myCameraManager.getCameraIdList();
            myCameraCharacteristics = myCameraManager.getCameraCharacteristics(
                    myCameraIdList[0]);
            myCameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
            myCameraManager.openCamera(myCameraIdList[0], stateCallback, handler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private void startPreview(CameraDevice camera) throws CameraAccessException {
        SurfaceTexture texture = myPreviewView.getSurfaceTexture();
        texture.setDefaultBufferSize(myPreviewView.getWidth(), myPreviewView.getHeight());
        Surface surface = new Surface(texture);
        try {
            myPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        myPreviewBuilder.addTarget(surface);
        camera.createCaptureSession(Arrays.asList(surface), mySessionStateCallback, myCameraHandler);
    }

}
